package be.kdg.bombermanunlimited.client.gui.chat;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

import javax.swing.JFrame;

import be.kdg.bombermanunlimited.client.CentralRegistry;
import be.kdg.bombermanunlimited.client.CentralRegistry.BombermanObjects;
import be.kdg.bombermanunlimited.client.network.NetworkFactory;
import be.kdg.bombermanunlimited.server.network.interfaces.INetworkManager;

public class ChatTest {

	public static INetworkManager nm;

	public ChatTest(String ip, int port) {
		JFrame frame = new JFrame("Chat test " + ip + ":" + port);
		ChatPanel chat = new ChatPanel();

		frame.setContentPane(chat);
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);

		frame.addWindowListener(new WindowListener() {

			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowClosing(WindowEvent e) {
				NetworkFactory.getNetworkFactory().getRingNetwork()
						.disconnect();
			}

			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub

			}
		});
	}

	private static String getLocalIpAddress() {
		try {
			Enumeration<NetworkInterface> tmp = NetworkInterface
					.getNetworkInterfaces();
			List<NetworkInterface> interfaces = Collections.list(tmp);
			List<String> possibleAddresses = new ArrayList<String>();

			for (int i = 0; i < interfaces.size(); i++) {
				NetworkInterface ni = interfaces.get(i);
				if (ni.isUp()) {
					String ip = "";
					List<InterfaceAddress> addresses = ni
							.getInterfaceAddresses();

					for (int j = 0; j < addresses.size(); j++) {
						if (addresses.get(j).getAddress().getHostAddress()
								.indexOf(":") == -1) {
							ip = addresses.get(j).getAddress().getHostAddress();
						}
					}

					if (!ip.equals("")) {
						possibleAddresses.add(ip);
						// System.out.println(possibleAddresses.size() + ") "
						//		+ ni.getDisplayName() + ": " + ip);
					}
				}
			}

			// System.out.print("Select interface: ");
			int index = -1;
			try {
				String in = new BufferedReader(new InputStreamReader(System.in))
						.readLine();
				index = Integer.parseInt(in);
			} catch (IOException e) {
				e.printStackTrace();
			}

			return (index > 0 && index <= possibleAddresses.size() ? possibleAddresses
					.get(index - 1) : "");
		} catch (SocketException e) {
			e.printStackTrace();
		}

		return "";
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length != 2 && args.length != 3) {
			System.out.println("2 parameters needed: managerIp managerPort");
		} else {
			String localIp = getLocalIpAddress();
			CentralRegistry.getRegistry().putObject(BombermanObjects.LOCAL_IP,
					localIp);
			// System.out.println("Local ip set to: "
			//		+ CentralRegistry.getRegistry().getString(
			//				BombermanObjects.LOCAL_IP));

			// System.out.println("Manager: " + args[0] + ":" + args[1]);
			String ip = args[0];
			int port = Integer.parseInt(args[1]);
			int ringPort = 0;
			if (args.length == 3) {
				ringPort = Integer.parseInt(args[3]);
			} else {
				// System.out.print("Port (0 = default): ");
				try {
					String in = new BufferedReader(new InputStreamReader(
							System.in)).readLine();
					ringPort = Integer.parseInt(in);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			CentralRegistry.getRegistry().putObject(BombermanObjects.RING_PORT, ringPort);
			CentralRegistry.getRegistry().putObject(BombermanObjects.HOST_IP, ip);
			CentralRegistry.getRegistry().putObject(BombermanObjects.RING_PORT, port);
			
			NetworkFactory.createNetworkFactory(ip, port);
			NetworkFactory.getNetworkFactory().createRingNetwork(ringPort);

			new ChatTest(CentralRegistry.getRegistry().getString(
					BombermanObjects.LOCAL_IP), ringPort);
		}
	}
}
